var polling = {
    url:'',
    flag:false, //轮询开关
    class:'',//
    docMessageId:'',
    docPhoneId:"",
    messageNum:'',
    phoneNum:'',
    startPollingt: function(){ //开始轮询
        window.setInterval(function(){
            if(polling.url) {
                ajaxLunXun({
                    url: polling.url,              //请求地址
                    type: "Post",                       //请求方式
                    dataType: "json",
                    success: function (data) {
                       var docMessage = document.getElementById(polling.docMessageId);
                       var docPhone = document.getElementById(polling.docPhoneId);
                       data = JSON.parse(data); //解析数据
                        if(data.messageflag>0){
                            polling.messageNum = data.messageflag;
                            docMessage.innerHTML = '留言本新增<strong style="color: #ff0000;font-size: 200%">'+polling.messageNum+'</strong>条消息';
                            if(docMessage.style.display=='none') {
                                docMessage.style.display = 'block';
                            }
                        }
                        if(data.phoneflag>0){
                            polling.phoneNum = data.phoneflag;
                            docPhone.innerHTML = '电话号码新增<strong style="color: #ff0000;font-size: 200%">'+polling.phoneNum+'</strong>条消息';
                            if(docPhone.style.display=='none') {
                                docPhone.style.display = 'block';
                            }
                        }
                    },
                    fail: function (status) {
                        // 此处放失败后执行的代码
                    }
                });
            }
        },5000);
    }
};
function ajaxLunXun(options) {
    options = options || {};
    options.type = (options.type || "GET").toUpperCase();
    options.dataType = options.dataType || "json";
    var params = formatParams(options.data); //格式化传递参数
    //创建 - 非IE6 - 第一步
    if (window.XMLHttpRequest) {
        var xhr = new XMLHttpRequest();
    } else { //IE6及其以下版本浏览器
        var xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }

    //接收 - 第三步
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) { //响应就绪
            var status = xhr.status;
            if (status >= 200 && status < 300) {
                options.success && options.success(xhr.responseText);
            } else {
                options.fail && options.fail(status);
            }
        }
    }

    //连接 和 发送 - 第二步
    if (options.type == "GET") {
        xhr.open("GET", options.url + "?" + params, true);
        xhr.send(null);
    } else if (options.type == "POST") {
        xhr.open("POST", options.url, true);
        //设置表单提交时的内容类型
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(params);
    }
}
//格式化参数
function formatParams(data) {
    var arr = [];
    for (var name in data) {
        arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
    }
    arr.push(("v=" + Math.random()).replace(".",""));
    return arr.join("&");
}

